package com.neusoft.bizcore.notice.system.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.notice.system.dto.NoticeConnectUserDTO;
import com.neusoft.bizcore.notice.system.model.NoticeConnectUser;
import com.neusoft.bizcore.notice.system.repository.NoticeConnectUserRepository;
import com.neusoft.bizcore.web.dto.AbstractConverter;

@Component
public class NoticeConnectUserConverter extends AbstractConverter<NoticeConnectUser, NoticeConnectUserDTO> {
    @Autowired
    private NoticeConnectUserRepository connectUserRepository;

    @Override
    public NoticeConnectUser toModel(final NoticeConnectUserDTO dto) {

        NoticeConnectUser model = this.connectUserRepository.findByUser(dto.getUser());
        if (model == null) {
            model = new NoticeConnectUser();
            model.setUser(dto.getUser());
            return this.connectUserRepository.save(model);
        }
        return model;
    }

    @Override
    public NoticeConnectUserDTO toDTO(final NoticeConnectUser model, final boolean forListView) {
        final NoticeConnectUserDTO dto = new NoticeConnectUserDTO();
        dto.setId(model.getId());
        dto.setUser(model.getUser());
        return dto;
    }
}
